package com.ruoyi.project.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.project.domain.ExpenseClaim;
import com.ruoyi.project.mapper.ContractMapper;
import com.ruoyi.project.domain.Contract;
import com.ruoyi.project.service.IContractService;

/**
 * 合同信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-06
 */
@Service
public class ContractServiceImpl implements IContractService 
{
    @Autowired
    private ContractMapper contractMapper;

    /**
     * 查询合同信息
     * 
     * @param contractId 合同信息主键
     * @return 合同信息
     */
    @Override
    public Contract selectContractByContractId(Long contractId)
    {
        return contractMapper.selectContractByContractId(contractId);
    }

    /**
     * 查询合同信息列表
     * 
     * @param contract 合同信息
     * @return 合同信息
     */
    @Override
    public List<Contract> selectContractList(Contract contract)
    {
        return contractMapper.selectContractList(contract);
    }

    /**
     * 新增合同信息
     * 
     * @param contract 合同信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertContract(Contract contract)
    {
        int rows = contractMapper.insertContract(contract);
        insertExpenseClaim(contract);
        return rows;
    }

    /**
     * 修改合同信息
     * 
     * @param contract 合同信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateContract(Contract contract)
    {
        contractMapper.deleteExpenseClaimByContractId(contract.getContractId());
        insertExpenseClaim(contract);
        return contractMapper.updateContract(contract);
    }

    /**
     * 批量删除合同信息
     * 
     * @param contractIds 需要删除的合同信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteContractByContractIds(Long[] contractIds)
    {
        contractMapper.deleteExpenseClaimByContractIds(contractIds);
        return contractMapper.deleteContractByContractIds(contractIds);
    }

    /**
     * 删除合同信息信息
     * 
     * @param contractId 合同信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteContractByContractId(Long contractId)
    {
        contractMapper.deleteExpenseClaimByContractId(contractId);
        return contractMapper.deleteContractByContractId(contractId);
    }

    /**
     * 新增合同报销信息
     * 
     * @param contract 合同信息对象
     */
    public void insertExpenseClaim(Contract contract)
    {
        List<ExpenseClaim> expenseClaimList = contract.getExpenseClaimList();
        Long contractId = contract.getContractId();
        if (StringUtils.isNotNull(expenseClaimList))
        {
            List<ExpenseClaim> list = new ArrayList<ExpenseClaim>();
            for (ExpenseClaim expenseClaim : expenseClaimList)
            {
                expenseClaim.setContractId(contractId);
                list.add(expenseClaim);
            }
            if (list.size() > 0)
            {
                contractMapper.batchExpenseClaim(list);
            }
        }
    }
}
